import { Module, Logger } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { UserController } from './user/user.controller'
import { UserService } from './user/user.service'
import { WillesModule } from './willes/willes.module'
import { ValidatorModule } from './pipe/validator/validator.module'
import { ValidatorController } from './pipe/validator/validator.controller'
import { ValidatorService } from './pipe/validator/validator.service'
import { RoleModule } from './role/role.module'
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'
import { UserModule } from './user/user.module'
import { LogsModule } from './logs/logs.module'
import { AuthModule } from './auth/auth.module'
import * as dotenv from 'dotenv'
import * as path from 'path'
const envFilePath = `.env.${process.env.NODE_ENV || `development`}`

@Module({
  imports: [
    ConfigModule.forRoot({ envFilePath, isGlobal: true, load: [() => dotenv.config({ path: '.env' })] }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) =>
        ({
          type: 'mysql',
          host: configService.get('DB_HOST'),
          port: +configService.get('DB_PORT'),
          username: configService.get('DB_USERNAME'),
          password: configService.get('DB_PWD'),
          database: configService.get('DB_DATABASE'),
          entities: [path.join(__dirname, '**/*.entity{.ts,.js}')],
          synchronize: true
          // logging: true
        }) as TypeOrmModuleOptions
    }),
    WillesModule,
    ValidatorModule,
    RoleModule,
    UserModule,
    LogsModule,
    AuthModule
  ],
  controllers: [AppController, UserController, ValidatorController],
  providers: [AppService, UserService, ValidatorService, Logger],
  exports: []
})
export class AppModule {}
